#include <bits/stdc++.h>
// 2025/01/22
// tag:
// Author: Zhang Muen
using namespace std;

int n, t[10001];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int p, cnt = 0;
        cin >> p >> t[i];
        cin >> p;
        while (p != 0)
        {
            cnt = max(cnt, t[p]);
            cin >> p;
        }
        t[i] += cnt;
        ans = max(ans, t[i]);
    }
    cout << ans;
    return 0;
}